import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex)

//统一定义管理公共数据(*)
const hasState = sessionStorage.state;
// console.log("hasState ", hasState)
const state = (hasState !== undefined) ? JSON.parse(hasState) : {
    users: {
        "2020210433": {
            name: "赵总",
            pwd: 'boss',
            role: 0
        },
        "2020210432": {
            name: "赵组",
            pwd: 'leader1',
            group: "001",
            role: 1
        },
        "2020210431": {
            name: "钱组",
            pwd: 'leader2',
            group: "001",
            role: 1
        },
        "2020210430": {  //工号
            name: "孙工",
            pwd: '2020210430',
            group: "001",
            role: 2,  //0boss 1leader 2maintainer 3monitor
        },
        "2020210429": {
            name: "李工",
            pwd: '2020210429',
            group: "001",
            role: 2,
        },
        "2020210428": {
            name: "周工",
            pwd: '2020210428',
            group: "001",
            role: 2,
        },
        "2020210427": {
            name: "吴工",
            pwd: '2020210427',
            group: "002",
            role: 3,
        },
        "2020210426": {
            name: "郑工",
            pwd: '2020210426',
            group: "002",
            role: 3,
        },
        "2020210425": {
            name: "王工",
            pwd: '2020210425',
            group: "002",
            role: 3,
        }
    },
    devices: {
        "2020210432-a-001": {
            id: "2020210432-a-001",
            name: "M3内喷电机",
            line: "2020210432-a",
            sensors: ["1000004700060003", "1000004700040003"],
            status: 0,  //设备状态：0正常（无工单或工单状态为4） 1异常（有工单且工单状态为0） 2维修中（有工单且工单状态>0 <4）
            sheetId: "645fac0e8be284014ecb837e",
        },
        "2020210432-a-002": {
            id: "2020210432-a-002",
            name: "工控机柜",
            line: "2020210432-a",
            sensors: ["1000004700080003"],
            status: 0,
            sheetId: "",
        },
        "2020210432-a-003": {
            id: "2020210432-a-003",
            name: "M5内喷电机",
            line: "2020210432-a",
            sensors: ["1000004700070003", "1000004700030003", "1000004700020003"],
            status: 0,
            sheetId: "",
        },
        "2020210432-a-004": {
            id: "2020210432-a-004",
            name: "M3内喷电机",
            line: "2020210432-a",
            sensors: ["1000004700060003", "1000004700040003"],
            status: 0,  //设备状态：0正常（无工单或工单状态为4） 1异常（有工单且工单状态为0） 2维修中（有工单且工单状态>0 <4）
            sheetId: "",
        },
        "2020210432-a-005": {
            id: "2020210432-a-005",
            name: "工控机柜",
            line: "2020210432-a",
            sensors: ["1000004700080003"],
            status: 0,
            sheetId: "",
        },
        "2020210432-a-006": {
            id: "2020210432-a-006",
            name: "M5内喷电机",
            line: "2020210432-a",
            sensors: ["1000004700070003", "1000004700030003", "1000004700020003"],
            status: 0,
            sheetId: "",
        },
        "2020210432-b-001": {
            id: "2020210432-b-001",
            name: "旁边墙上工控机柜",
            line: "2020210432-b",
            sensors: ["1000004700080004"],
            status: 0,
            sheetId: "",
        },
        "2020210432-b-002": {
            id: "2020210432-b-002",
            name: "靠近立体库机床的外置电机",
            line: "2020210432-b",
            sensors: ["1000004700070002", "1000004700030002", "1000004700040002", "1000004700020002"],
            status: 0,
            sheetId: "",
        },
        "2020210432-b-003": {
            id: "2020210432-b-003",
            name: "立体库",
            line: "2020210432-b",
            sensors: ["1000004700100002", "1000004700050003"],
            status: 0,
            sheetId: "",
        },
    },
}

//唯一的取值方法，计算属性
const getters = {
    getUserById(state){
        return id => state.users[id];
    },
    getAllUser(state){  //获取除了boss和leader之外的用户
        let Users = [];
        let users = state.users;
        for(let user in users){  // 用 let x in 对象时，x是对象中每个属性的键（而非对象中每个属性）
            if(users[user].role === 0 || users[user].role === 1) break;
            Users.push(users[user])
        }
        return Users;
    },
    getAllMaintain(state){
        return Object.values(state.users).filter(elem => {
            return elem.role === 2;
        })
    },
    getDeviceById(state){
        return id => state.devices[id]  //根据对象属性key获取其value
    }
}

//唯一修改值的方法，同步阻塞（*）
const mutations = {  //调mutations中的方法必须用actions中定义
    updateUser(state, id, user){
        state.users[id] = user;
    },
    updataDeviceSheet(state, deviceId, sheetId){
        state.devices[deviceId].sheetId = sheetId
    },
    updataDeviceStatus(state, deviceId, status){
        state.devices[deviceId].stutus = status
    }
}

//异步方法
const actions = {  //调actions中的方法必须dispatch
    asyncUpdateUser(context, id, user){
        context.commit('updateUser', id, user)  //context指的是整个对象
    },
    asyncUpdataDeviceSheet(context, deviceId, sheetId){
        context.commit('updataDeviceSheet', deviceId, sheetId)
    },
    asyncUpdataDeviceStatus(context, deviceId, status){
        context.commit('updataDeviceStatus', deviceId, status)
    }
}

//拆分模块
const modules = {

}

export default new Vuex.Store({  //这里的Store必须大写
    state,
    getters,
    mutations,
    actions,
    modules
})
